package com.best.testprogressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

/**
 * Created by FuKaiqiang on 2018-01-21.
 */

public class RoundProgressBarWidthProgress extends HorizontalProgressBarWithProgress {

    private int mRadius = dp2px(30);
    private int mMaxPaintWidth; //用来计算宽度

    public RoundProgressBarWidthProgress(Context context) {
        this(context, null);
    }

    public RoundProgressBarWidthProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBarWidthProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mReachHeight = (int) (mUnReachHeight * 2.5f); //为了好看，没有实际意义

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBarWidthProgress);
        mRadius = (int) typedArray.getDimension(R.styleable.RoundProgressBarWidthProgress_radius, mRadius);
        typedArray.recycle();

        mPaint.setStyle(Paint.Style.STROKE); //只绘制图形轮廓
        mPaint.setAntiAlias(true); //锯齿
        mPaint.setDither(true); //防抖动
        //(可设 可不设)设置线冒样式，取值有Cap.ROUND(圆形线冒)、Cap.SQUARE(方形线冒)、Paint.Cap.BUTT(无线冒)
        mPaint.setStrokeCap(Paint.Cap.ROUND);

    }

    /**
     * 测量
     *
     * @param widthMeasureSpec  宽度的Spec
     * @param heightMeasureSpec 高度的Spec
     */
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMaxPaintWidth = Math.max(mReachHeight, mUnReachHeight);
        //因为是圆形默认四个padding一致；except期望值，如果用户选择wrap的话
        int except = mRadius * 2 + mMaxPaintWidth + getPaddingLeft() + getPaddingRight();
        int width = resolveSize(except, widthMeasureSpec); //同measureHeight方法
        int height = resolveSize(except, heightMeasureSpec);
        //最终的宽的大小
        int readWidth = Math.min(width, height);

        mRadius = (readWidth - getPaddingLeft() - getPaddingRight() - mMaxPaintWidth) / 2;

        setMeasuredDimension(readWidth, readWidth);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text = getProgress() + "%";
        float textWidth = mPaint.measureText(text);
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;

        canvas.save();

        canvas.translate(getPaddingLeft() + mMaxPaintWidth / 2, getPaddingTop() + mMaxPaintWidth / 2);
        mPaint.setStyle(Paint.Style.STROKE);
        //draw unreach bar
        mPaint.setColor(mUnReachColor);
        mPaint.setStrokeWidth(mUnReachHeight);
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);

        //draw reach bar
        mPaint.setColor(mReachColor);
        mPaint.setStrokeWidth(mReachHeight); //设置空心宽度
        //弧度
        float sweepAngle = getProgress() * 1.0f / getMax() * 360;
        canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2),
                0, sweepAngle, false, mPaint);
        //draw text
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(text,mRadius-textWidth/2,mRadius-textHeight,mPaint);
        canvas.restore();
    }
}
